#include "../Helix/View/OpenGL/OpenGLSupport.h"

#include "../Helix/Helix.h"
#include "../Helix/Input.h"
#include "../Helix/Timer.h"
#include "../Helix/Math/Constants.h"
#include "../Helix/Math/Matrix4x4.h"
#include "Game.h"					// Header File For Derived Application Class
#include <math.h>

using namespace helix;

void Game::renderScene()
{
	glMatrixMode(GL_MODELVIEW);
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
	
	
	//this is where everything in the main rendering loop should go
	glLoadIdentity();			//Reset the modelview matrix to the identity matrix

	//Translate the cube using the value from mouse input
	glTranslatef(0.0,0.0,zoom);

	//Rotate the cube with the values captured by the mouse.
	glRotatef(rotY,1.0,0.0,0.0);
	glRotatef(rotX,0.0,1.0,0.0);

	glPushMatrix();


	glMultMatrixd(base->getTransformationMatrix().Transpose().getMatrix());
	//Vector pos = base->getPosition();
	//glTranslatef(pos.x(),pos.y(),pos.z());
	//glTranslatef(0.0,-10.0,0.0);
	openGLHelper->drawMesh(floor);

	glPopMatrix();

	
	for(int i=0;i<NUM_BOXES;i++)
	{
		glPushMatrix();
		glMultMatrixd(objects[i]->getTransformationMatrix().Transpose().getMatrix());
		//Vector pos = objects[i]->getPosition();
		//glTranslatef(pos.x(),pos.y(),pos.z());
		openGLHelper->drawMesh(box);
		glPopMatrix();
	}
	
	glFlush();
}

void Game::update(double dSeconds)
{
	physics->update(dSeconds);

	float diffX = input->getMouseX();
	float diffY = input->getMouseY();

	if(input->leftMouse())
	{
		rotX+= diffX * DEGTORAD * 20.0f;
		rotY+= diffY * DEGTORAD * 20.0f;
	}
	else if(input->rightMouse())
	{
		zoom-=diffY*0.05f;
	}

	if(input->keyDown(H_SPACE))
	{
		resetSimulation();
	}

}
